/*
 * I write in my free time, for personal using
 * when i fell proud of it, i will give it to public
 * now,have nothing more to say
 */
package com.quangtin3.utils;

import java.io.FileInputStream;
import java.util.Properties;

/**
 *
 * @author quanghx
 */
public class TunllConfig {

    private static TunllConfig config;
    public static final String configFile = "etc/tunll.cfg";
    private int nServerPort;
    private String log4jPropertiesFile;
    //Default value
    private static final int SERVER_PORT = 3105;

    public int getServerPort() {
        return nServerPort;
    }

    public String getLog4jPropertiesFile() {
        return log4jPropertiesFile;
    }

    public static TunllConfig getConfig() {
        try {
            if (config == null) {
                config = new TunllConfig();
            }
        } catch (Exception ex) {
            TunllLogger.sys.fatal(ex);
        }
        return config;
    }

    private TunllConfig() throws TunllException {
        Properties properties = new Properties();
        try {
            FileInputStream propsFile = new FileInputStream(configFile);

            properties.load(propsFile);
            propsFile.close();

            nServerPort = analyzNumber(
                    "serverPort", properties.getProperty("serverPort"), SERVER_PORT);

            log4jPropertiesFile = properties.getProperty("log4jPropertiesFile");

        } catch (Exception e) {
            throw new TunllException(e);
        }
    }

    private int analyzNumber(String name, String sNumber, int iDefalt) {
        int iValue;
        try {
            iValue = Integer.parseInt(sNumber);
        } catch (NumberFormatException ex) {
            iValue = iDefalt;
            TunllLogger.sys.debug(
                    "config error in " + name + " " + sNumber + ", set to default value " + iDefalt);
        }
        return iValue;
    }
}
